<?php
/*------------------------------------------------------------------------
# Teline III - Version testing - Licence Owner JOOM
# ------------------------------------------------------------------------
# Copyright (C) Lifetime Joomla Just for Sharing, Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: Joomla Just for Sharing
# Websites: http://www.joomlart.com - http://www.joomlancers.com
# This file may not be redistributed in whole or significant part.
-------------------------------------------------------------------------*/ 



// no direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');


class widgetBase
{
	/**
	 * Location of service API
	 *
	 * @var string $api_url
	 */
	var $api_url 		= "";
	var $api_key 		= "";
	var $params 		= array();
	var $method 		= "curl";
	var $response 		= "xml";

	var $sData			= "";
	var $aData			= array();

	function widgetBase()
	{

	}

	function setParams($var, $value)
	{
		$this->$var = $value;
	}

	function makeRequest()
	{
		//get and process data response
		$this->_process();

		return $this->aData;
	}

	function _process()
	{
		$this->_buildUrl();

		switch ($this->method)
		{
			case "curl":
				$this->_curl($this->api_url, $this->sData);
				break;
			case "http":
				$this->sData = @trim(file_get_contents($this->api_url));
				break;
			case "rpc":
				//not supportted yet
				break;
		}

		//process data retrieve

		switch ($this->response)
		{
			case "html":
				$this->aData["data"] = $this->sData;
				break;
			case "json":
				$this->aData = $this->_json2array($this->sData);
				break;
			case "ini":
				$this->aData = $this->_ini2array($this->sData);
				break;
			case "xml":
				$xml = new JSimpleXML();
				$xml->loadString($this->sData);
				$this->_xml2array($xml->document, $this->aData);
				break;
		}
	}

	function _buildUrl()
	{
		if(strpos($this->api_url, '?') === false) {
			$this->api_url .= '?';
		}
		foreach ($this->params as $param => $value) {
			$this->api_url .= '&' . $param . '=' .urlencode($value);
		}
		//echo $this->api_url;
	}

	function _curl($url, &$output)
	{
		if (extension_loaded('curl')){
			//use curl to get content from url
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
			curl_setopt($ch, CURLOPT_TIMEOUT, 30);
			
			
			$output = @curl_exec($ch);
			
			$httpinfo = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	
			curl_close($ch);
	
			return $httpinfo;

		} else {
			$output = $this->getUrl($url, 80, '');
		}
	}

	function getUrl ($url, $port, $post) {
		$aURL = parse_url($url);
		
		$domain = $aURL['host'];
		$uri = $aURL['path'] . '?' . $aURL['query'];

		//get content by socket
		//parse $url to $domain and $uri
		//$domain, $uri, $post
		$header = '';
		$header .= "POST $uri HTTP/1.0\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Content-Length: " . strlen ($post) . "\r\n\r\n";
		$fp = @fsockopen ($domain, $port, $errno, $errstr, 30);
		
		if (!$fp)
			return false;
		fputs ($fp, $header . $post);
		$content = '';
		while (!feof($fp)){
			$content .= fgets ($fp, 1024);
		}
		
		list($sHeader, $sContent) = split("\r\n\r\n",$content, 2);
		$sContent = trim($sContent);
		if(substr($sContent, 0, 2) == "//") {
			$sContent = substr($sContent, 2);
		}
        return chop($sContent);
	}
	/**
	 * RESPONSE
	 */

	function _json2array($value)
	{
		$patterns = array ("\n", "\\x26", "\\x2F", "\\x3B" );
		$replace = array ("", "&", "/", ";" );
		$value = str_replace ( $patterns, $replace, $value );
		$value = trim($value);
		if(substr($value, 0, 2) == '//') {
			$value = substr($value, 2);
		}
		$value = trim($value);

		$arr = json_decode($value, true);
		return $arr;
	}

	function _ini2array($value)
	{
		return parse_ini_string($value, true);
	}

	function _xml2array($obj, &$arr)
	{
		if(is_object($obj) && $obj->name()) {
			$name = $obj->name();

			//more same child item => group those items
			if(isset($arr[$name])) {
				if(isset($arr[$name]['name'])) {
					//not group yet => start group
					$arrTemp = $arr[$name];
					unset($arr[$name]);

					$arr[$name][0] = $arrTemp;
				}

				$key = count($arr[$name]);
				$arr[$name][$key] = array();
				$arr[$name][$key]['name'] = $name;
				$arr[$name][$key]['data'] = $obj->data();
				$arr[$name][$key]['attributes'] = $obj->attributes();
				$arr[$name][$key]['level'] = $obj->level();

				if(count($obj->children()) > 0)
				{
					foreach ($obj->children() as $objChild) {
						$this->_xml2array($objChild, $arr[$name][$key]['children']);
					}
				}


			} else {
				$arr[$name] = array();

				$arr[$name]['name'] = $name;
				$arr[$name]['data'] = $obj->data();
				$arr[$name]['attributes'] = $obj->attributes();
				$arr[$name]['level'] = $obj->level();

				if(count($obj->children()) > 0)
				{
					foreach ($obj->children() as $objChild) {
						$this->_xml2array($objChild, $arr[$name]['children']);
					}
				}

			}
		}
	}

	/*function _xml2array($obj, &$arr)
	{
	if(is_object($obj)) {
	$arr['name'] = $obj->name();
	$arr['data'] = $obj->data();
	$arr['attributes'] = $obj->attributes();
	$arr['level'] = $obj->level();

	if(count($obj->children()) > 0)
	{
	foreach ($obj->children() as $objChild) {
	$this->_xml2array($objChild, $arr['children'][]);
	}
	}
	}
	}*/
}
?>